package com.dianrui.qiyouriding.activity;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dianrui.qiyouriding.Constant;
import com.dianrui.qiyouriding.R;
import com.dianrui.qiyouriding.adapter.StationListAdapter;
import com.dianrui.qiyouriding.bean.BikePointVehicle;
import com.dianrui.qiyouriding.dialog.StartCarDialog;
import com.dianrui.qiyouriding.interfaces.OnAskDialogCallBack;
import com.dianrui.qiyouriding.util.JsonUtils;
import com.dianrui.qiyouriding.okhttp.OkHttpRequest;
import com.dianrui.qiyouriding.util.StringUtils;
import com.dianrui.qiyouriding.util.ToastUtil;
import com.dianrui.qiyouriding.util.Url;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 *站点借车界面
 */

@SuppressWarnings("ALL")
public class BikePointListActivity extends BaseActivity {
    private List<BikePointVehicle> bikePointVehicleList = new ArrayList<>();
    private StationListAdapter stationListAdapter;
    private String mFixedId;
    private  Double lat;
    private Double lng;

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.recycleview)
    RecyclerView recyclerView;


    @Override
    public int getContentViewId() {
        return R.layout.bike_point_vehicle_list;
    }

    @Override
    public void initView() {
        customInit(true,R.color.main_color);
        if (getIntent() != null && getIntent().getExtras() != null) {
            Bundle bundle=getIntent().getExtras();
            if (bundle != null) {
                mFixedId=bundle.getString("fixed_id");
                lat = bundle.getDouble("lat");
                lng = bundle.getDouble("lng");
            }

        }
        title.setText(getString(R.string.bike_point_list_txt));
        LinearLayoutManager layoutManager = new LinearLayoutManager(BikePointListActivity.this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        stationListAdapter=new StationListAdapter(R.layout.item_bike_point_vehicle_list,bikePointVehicleList);
        recyclerView.setAdapter(stationListAdapter);
        networkVehicle();
        stationListAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                int itemViewId = view.getId();
                final BikePointVehicle bikePointVehicle=(BikePointVehicle)adapter.getItem(position);
                switch (itemViewId){
                    case R.id.use:
                        StartCarDialog.createStartCarDialog(BikePointListActivity.this, getString(R.string.start_car_txt), new OnAskDialogCallBack() {
                            @Override
                            public void onokey(Dialog dialog) {
                                dialog.dismiss();
                                Intent intent = new Intent(BikePointListActivity.this, ScanInfoActivity.class);
                                Bundle bundle = new Bundle();
                                bundle.putString("devicenumber", bikePointVehicle.number);
                                bundle.putString("vehicle_status", "2");
                                bundle.putString("isRomote", "2");
                                bundle.putDouble("lat",lat);
                                bundle.putDouble("lng",lng);
                                intent.putExtras(bundle);
                                startActivity(intent);
                                finish();
                            }

                            @Override
                            public void onCancel(Dialog dialog) {
                                dialog.dismiss();
                            }
                        });
                        break;
                        default:
                            break;
                }
            }
        });
    }

    @Override
    public void initData() {}


    @OnClick(R.id.back)
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            default:
                break;
        }

    }

     /**附近网点中的车辆**/
    private void networkVehicle() {
        JsonObject vehicleJson=new JsonObject();
        vehicleJson.addProperty("fixed_id", mFixedId);
        OkHttpRequest.getInstance().postRequests(Url.GETNETWORK_POINT , vehicleJson.toString(),  new OkHttpRequest.XCallBack() {
            @Override
            public void onResponse(String response) {
                if (!StringUtils.isEmpty(response)) {
                    try {
                        if (isFinishing()) {
                            return;
                        }
                        String status= JsonUtils.getJsonStr(response,Constant.STATUS);
                        if (Constant.DATASUCCESS.equals(status)) {
                            if (bikePointVehicleList!=null) {
                                bikePointVehicleList.clear();
                            }
                            String data=JsonUtils.getJsonStr(response,"data");
                            if (!StringUtils.isEmpty(data)) {
                                List<BikePointVehicle> list=JsonUtils.jsonStrToList(data, new TypeToken<ArrayList<BikePointVehicle>>(){}.getType());
                                if (list != null && list.size()>0) {
                                    bikePointVehicleList.addAll(list);
                                    stationListAdapter.setNewData(bikePointVehicleList);
                                }else {
                                    stationListAdapter.getData().clear();
                                    stationListAdapter.setEmptyView(LayoutInflater.from(BikePointListActivity.this).inflate(R.layout.empty_view,null));
                                    stationListAdapter.notifyDataSetChanged();
                                }
                            }
                        }else if (Constant.BACKLOGIN.equals(status)) {
                            JumpKillActivitys(LoginActivity.class);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    if (bikePointVehicleList.size() <= 0) {
                        stationListAdapter.getData().clear();
                        stationListAdapter.setEmptyView(LayoutInflater.from(BikePointListActivity.this).inflate(R.layout.empty_view,null));
                        stationListAdapter.notifyDataSetChanged();
                    } else {
                        recyclerView.setVisibility(View.VISIBLE);
                    }
                }
            }

            @Override
            public void failedRequest(String result) {
                ToastUtil.showToast(getString(R.string.net_error));
            }
        });
    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (bikePointVehicleList != null) {
            bikePointVehicleList.clear();
        }
    }
}
